package com.ltedu.system.controller;

import com.ltedu.common.enums.ErrorCodeEnum;
import com.ltedu.common.utils.JwtUtils;
import com.ltedu.common.validate.Create;
import com.ltedu.common.validate.Update;
import com.ltedu.common.vo.Result;
import com.ltedu.system.domain.dto.MenuNode;
import com.ltedu.system.domain.vo.SysMenuVO;
import com.ltedu.system.domain.vo.TreeSelect;
import com.ltedu.system.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单信息 前端控制器
 * </p>
 *
 * @author Jerry
 * @since 2020-11-27
 */
@RestController
@RequestMapping("/system/menu")
@CrossOrigin
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 添加菜单
     *
     * @param sysMenuVo
     * @return {@link Result}
     */
    @PostMapping("/add")
    public Result add(@Validated(Create.class) @RequestBody SysMenuVO sysMenuVo) {
        return Result.ok(sysMenuService.save(sysMenuVo));
    }

    /**
     * 修改菜单
     *
     * @param sysMenuVo
     * @return {@link Result}
     */
    @PutMapping("/update")
    public Result update(@Validated(Update.class) @RequestBody SysMenuVO sysMenuVo) {
        return Result.ok(sysMenuService.update(sysMenuVo));
    }

    /**
     * 根据id获取菜单信息
     *
     * @param id id
     * @return {@link Result}
     */
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        return Result.ok(sysMenuService.getById(id));
    }

    /**
     * 根据id删除菜单信息
     *
     * @param id id
     * @return {@link Result}
     */
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        if (sysMenuService.hasChildByMenuId(id)) {
            return Result.fail(ErrorCodeEnum.EXIST_SUBMENU.getMessage());
        }
        if (sysMenuService.checkMenuExistRole(id)) {
            return Result.fail(ErrorCodeEnum.MENU_ASSIGNED.getMessage());
        }
        return Result.ok(sysMenuService.removeById(id));
    }

    /**
     * 批量删除
     *
     * @param idList id列表
     * @return {@link Result}
     */
    @DeleteMapping("/removeBatchs")
    public Result removeBatchs(@RequestBody List<Long> idList) {
        return Result.ok(sysMenuService.removeByIds(idList));
    }

    /**
     * 菜单树列表
     *
     * @return {@link Result}
     */
    @GetMapping("/tree")
    public Result tree() {
        return Result.ok(sysMenuService.treeList());
    }

    /**
     * 菜单树
     *
     * @return {@link Result}
     */
    @GetMapping("/menuTree")
    public Result menuTree() {
        List<MenuNode> menuNodeList = sysMenuService.treeList();
        List<TreeSelect> treeSelectList = sysMenuService.buildMenuTreeSelect(menuNodeList);
        return Result.ok(treeSelectList);
    }

    /**
     * 角色菜单分配
     *
     * @param roleId  角色id
     * @param request 请求
     * @return {@link Result}
     */
    @GetMapping("/roleMenutree/{roleId}")
    public Result roleMenutree(@PathVariable Long roleId, HttpServletRequest request) {
        List<MenuNode> menus = sysMenuService.treeList();
        List<Long> roleMenutreeList = sysMenuService.roleMenutreeList(roleId);
        List<TreeSelect> treeSelectList = sysMenuService.buildMenuTreeSelect(menus);
        Map<String, Object> result = new HashMap<>();
        result.put("checkedKeys", roleMenutreeList);
        result.put("menus", treeSelectList);
        return Result.ok(result);
    }

    /**
     * 菜单路由
     *
     * @param request 请求
     * @return {@link Result}
     */
    @GetMapping("/getRouters")
    public Result getRouters(HttpServletRequest request) {
        Long userId = JwtUtils.getJwtTokenByMemberId(request).longValue();
        List<MenuNode> menuNodeList = sysMenuService.getMenuTreeByUserId(userId);
        return Result.ok(sysMenuService.buildMenu(menuNodeList));
    }
}

